<?php
/*
PluginName:最新评论
PluginUrl:http://www.lisijie.org
PluginAuthor:lisijie
PluginAuthorUrl:http://www.lisijie.org
PluginDesc:最新评论widget for Jblog2
PluginVer:2.0.0
*/

class RecentcommentWidget extends Widget {
	
	function RecentcommentWidget() {
		$this->Widget(array('title'=>'最新评论'));
	}
	
	function display() {
		if ( !($data = $this->cache->get('widget_recentcomment')) ) {
			$data = '';
			$limit = $this->get_option('limit') ? $this->get_option('limit') : 10;
			$strlen = $this->get_option('length') ? $this->get_option('length') : 30;
			$query = $this->db->query("SELECT c.id, c.postid, c.author, c.content, c.dateline, p.title ,p.alias FROM ".tname('comment')." c JOIN ".tname('post')." p ON c.postid = p.id
			 WHERE c.status = 1 ORDER BY c.id DESC LIMIT $limit");
			while ( $row = $this->db->fetch_array($query) )  {
				$row['content'] = apply_filter('comment_content', $row['content']);
				$row['content'] = strip_tags($row['content']);
				$data .= '<li><a href="'.url("mod=post&id={$row[postid]}&alias={$row[alias]}").'#comment-'.$row['id'].'" title="'.$row['content'].'">'.cut_str($row['content'],$strlen).'</a></li>';
			}
			$data = '<ul>'.$data.'</ul>';
			$this->cache->set('widget_recentcomment', $data);
		}
		return $data;
	}
	
	function recache() {
		$GLOBALS['cache']->del('widget_recentcomment');	
	}
}
add_action('post_comment', array('RecentcommentWidget','recache'));
?>